package yunplus.io.hchome.display.activity;

import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import yunplus.io.hchome.R;
import yunplus.io.hchome.display.utils.ActivityUtils;
import yunplus.io.hchome.display.utils.ToastUtils;
import yunplus.io.hchome.display.view.ILaunchView;
import yunplus.io.hchome.display.view.ILoginView;
import yunplus.io.hchome.display.view.IWebAppView;
import yunplus.io.hchome.presenter.contract.ILaunchExtPresenter;
import yunplus.io.hchome.presenter.implement.LaunchExtPresenter;
import yunplus.io.hchome.utils.HandlerUtils;

public class LaunchActivity extends BaseActivity implements Runnable,ILaunchView {

    private final static String TAG = "LaunchActivity";

    ILaunchExtPresenter _mLaunchPresenter;

    AlertDialog.Builder _mNetworkAlertDialog;

    boolean _isLan = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        _mLaunchPresenter = new LaunchExtPresenter(this, this);
//        _isLan = _mLaunchPresenter.isLan();
//        if(_isLan){
//            _mLaunchPresenter.checkNetwork();
//        }else{
//            setNetworkDone();
//        }
        _mLaunchPresenter.checkNetwork();
    }

    @Override
    public void run() {
        if (ActivityUtils.isAlive(this)) {
            _mLaunchPresenter.launch();
        }
    }

    @Override
    public void goLoginWithWifi(boolean isWifi) {
        //传递网络状态
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra(ILoginView.INTENT_EXTRA_KEY_NETWORK, isWifi);
        startActivity(intent);
        finish();
    }

    @Override
    public void startAppFromLan(String domain, boolean isLan) {
        Intent intent = new Intent(this, WebviewActivity.class);
        intent.putExtra(IWebAppView.INTENT_EXTRA_KEY_DOMAIN, domain);
        intent.putExtra(IWebAppView.INTENT_EXTRA_KEY_LAN, isLan);
        startActivity(intent);
        finish();
    }

    public void setNetworkDone() {
        HandlerUtils.postDelayed(this, 100);
    }

    @Override
    public void checkNetworkStart() {
        ToastUtils.with(this).show("正在检测网络服务,请稍后...");
    }

    @Override
    public void useWifi() {
        setNetworkDone();
    }

    @Override
    public void useMobile() {
        setNetworkDone();
        //用户反馈不需要该提示信息
        /*
        _mNetworkAlertDialog = new AlertDialog.Builder(this);
        _mNetworkAlertDialog.setTitle("提示")
                .setMessage("检测到您使用的是手机网络,是否需要链接wifi?")
                .setCancelable(false)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        setNetwork();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        setNetworkDone();
                    }
                })
                .show();
                //*/
    }

    @Override
    public void unAvaliable() {
        _mNetworkAlertDialog = new AlertDialog.Builder(this);
        _mNetworkAlertDialog.setTitle("提示")
                .setMessage("未检测到您使用的网络,请前往设置wifi?")
                .setCancelable(false)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        setNetwork();
                    }
                })
                .show();
    }

    public void setNetwork() {
        Intent intent;
        try {
            int sdkVersion = android.os.Build.VERSION.SDK_INT;
            if (sdkVersion > 10) {
                intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
            } else {
                intent = new Intent();
                ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
                intent.setComponent(comp);
                intent.setAction("android.intent.action.VIEW");
            }
            this.startActivityForResult(intent, 1);
        } catch (Exception e) {
            ToastUtils.with(this).show("打开网络设置错误，请检查您的权限");
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(_mLaunchPresenter.isWifi()) {
            setNetworkDone();
        }else if(_mLaunchPresenter.isMobile()){
            setNetworkDone();
        }else{
            ToastUtils.with(this).show("您没有可用的网络连接,无法使用!");
        }

    }

}
